// pages/index/index.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    swiperinfo: {
      hgt: 0,
      indicatorDots: true,
      autoplay: true,
      circular: true,
      interval: 5000,
      duration: 500,
    },
    panlvis: 'hidden',
    showModel: false,   
    isaward:false,
    banners: [
      {
        imgurl: 'https://www.jubenxiong.com/games/lovepear0613/imgs/banner/index1.jpg',
        name: ''//新品-植物柔润凝萃
      },
      {
        imgurl: 'https://www.jubenxiong.com/games/lovepear0613/imgs/banner/index2.jpg',
        name: '' //初颜系列-微精华 亲呵护
      }
    ],
    swiperCurrent: 0,
    awardlist:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var storeid = 1;
    var oldstoreid = wx.getStorageSync('storeid', storeid);
    if (!options.hasOwnProperty('storeid')) {
      //跳转到选择页面
      wx.redirectTo({
        url: '../search/search',
      });
      return;
    }
    else{
      storeid = options.storeid;
      wx.setStorageSync('storeid', storeid);
      console.log('aabbcc storeid=' + storeid);

      var that = this;
      setTimeout(function(){
        that.setData({ panlvis: 'visible' });
      }, 500);
    }

    console.log('oldstoreid=' + oldstoreid + 'storeid=' + storeid);

    // if (oldstoreid != storeid) {
    //   wx.setStorageSync('token', '');
    //   wx.setStorageSync('uid', '');
    //   wx.setStorageSync('ukey', '');
    // }

    wx.setStorageSync('token', '');
    wx.setStorageSync('uid', '');
    wx.setStorageSync('ukey', '');
    app.globalData.storeid = storeid;

    this.checkLogin();
    var that = this;
    wx.getSystemInfo({
      success: function (res) {
        var swiper_hgt = 480 * res.windowWidth / 1000;
        var swiperinfo = that.data["swiperinfo"];
        swiperinfo.hgt = swiper_hgt;
        var item_width1 = res.windowWidth - 30;
        var item_width2 = (item_width1 - 15) * 0.5;
        var item_height = (res.windowHeight - swiper_hgt - 30 - 40) * 0.4 - 32;
        var temp_hgt = item_width2 * 1.2;
        if (item_height * 2 > temp_hgt) {
          item_height = temp_hgt * 0.5;
        }
        that.setData({
          swiperinfo: swiperinfo,
          win_width: res.windowWidth,
          item_wtd1: item_width1,
          item_wtd2: item_width2,
          item_hgt: item_height
        });
      },
    });

    var url = app.globalData.webUrl + 'Store/consoleIndex';
    app.loadDataFromServer(url, this.loadIndexCallBack, {});

    var storeurl = app.globalData.webUrl + 'Store/storeinfo';
    app.loadDataFromServer(storeurl, this.loadStoreinfoBack, { storeid: storeid });

    
  },
  loadIndexCallBack: function (res) {

    wx.hideToast();
    if (res.status != 'ok') {
      wx.showToast({
        title: '网络错误~',
        image: '../imgs/tips.png'
      });
      return;
    }
    var adlist = res.result;

    for (var i = 0; i < adlist.length; i++) {
      adlist[i].icon = app.globalData.ImgwebUrl + adlist[i].icon;
    }

    this.setData({
      banners: adlist
    });

  },
  loadStoreinfoBack: function (res) {
    app.globalData.storeInfo = res.result;
    // app.globalData.userinfo.storename = app.globalData.storeInfo.name;
    wx.setNavigationBarTitle({
      title: app.globalData.storeInfo.name
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.hideLoading();
   
    if (app.globalData.storeInfo){
      wx.setNavigationBarTitle({
        title: app.globalData.storeInfo.name
      })
    }

    if (app.globalData.userinfo) {
      console.log('aabbcceeff');
    }
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  togoods: function (e) {
    console.log('to page ')
    var goodsid = e.currentTarget.dataset.goodsid;
    console.log('to page goodsid =' + goodsid);
    var gtype = e.currentTarget.dataset.gtype;
    var linktype = e.currentTarget.dataset.linktype;

    console.log('linktype=' + linktype);

    if (linktype == 1){
      var tourl = '../projects/detail/detail?goodsid=' + goodsid;
      if (gtype == 2) {
        tourl = '../activity/detail/actdetail?goodsid=' + goodsid;
      }
      wx.navigateTo({
        url: tourl,
      });
    }
    else{
      wx.navigateTo({
        url: '../contactus/contactus',
      });
    }

    
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return app.getShareObj();
  },
  swiperChange: function (e) {
    if (e.detail.source == "touch") {
      //防止swiper控件卡死  
      if (this.data.current == 0) {// 卡死时，重置current为正确索引
        this.setData({
          swiperCurrent: 0
        });
      }
      else {//正常轮转时，记录正确页码索引  
        this.setData({
          swiperCurrent: e.detail.current
        });
      }
    }
  },
  SelectMenu: function (e) {
    var menutype = e.currentTarget.dataset.menutype;
    if (menutype == 'project') {
      wx.navigateTo({
        url: '../projects/projectsindex',
      });
    }
    else if (menutype == 'activity1') {
      wx.navigateTo({
        url: '../activity/activity?ptype=1',
      });
    }
    else if (menutype == 'activity2') {
      wx.navigateTo({
        url: '../activity/activity?ptype=2',
      });
    }
    else if (menutype == 'appoint') {
      wx.navigateTo({
        url: '../appoint/appoint',
      });
    }
    else if (menutype == 'personal') {
      wx.navigateTo({
        url: '../my/myindex',
      });
    }
    else if (menutype == 'contact') {
      wx.navigateTo({
        url: '../contactus/contactus',
      });
    }
  },
  //------------------------授权check----------------------------------
  agreeGetUser: function (e) {
    //console.log(e);
    //设置用户信息本地存储
    try {
      wx.setStorageSync('userInfo', e.detail.userInfo);
    }
    catch (e) {
      wx.showToast({
        title: '系统提示:网络错误',
        icon: 'warn'
      });
    }
    wx.showLoading({
      title: '加载中...'
    });
    let that = this;
    console.log('isfalse');
    that.setData({
      showModel: false
    });
    if (e.detail.userInfo != null && e.detail.userInfo != undefined) {
      app.getOP(e.detail.userInfo);
    }
    else{
      wx.hideLoading();
    }
  },
  checkLogin: function () {
    app.checkLogin(this.UserInfoBack);
  },
  UserInfoBack:function(res){
    app.loadCallBack(res);
    console.log('back');
    console.log(res);
    var isaward = false;
    if (app.globalData.awardlist.length>0){
      isaward = true;
    }
    this.setData({
      awardlist:app.globalData.awardlist,
      isaward: isaward
    });
  },
  getAward:function(){
    var url = app.globalData.webUrl + 'Games/getGameReward';

    //grewardid
    var userid = wx.getStorageSync('uid');
    
    var alist = this.data.awardlist;
    var alistarr = [];
    if (alist.length<=0)
      return;

    for(var i=0;i<alist.length;i++){
      alistarr.push(alist[i].awardid);
    }

    var aliststr = alistarr.join(",")
    var todata = { userid: userid, grewardid: aliststr };

    app.loadDataFromServer(url, this.loadGetAwardBack, todata);
  },
  loadGetAwardBack:function(res){
    wx.hideToast();
    if (res.status != 'ok') {
      wx.showToast({
        title: res.msg,
        icon: 'none'
      });
      return;
    }

    wx.showToast({
      title: '领取成功,请在购买列表里面查看',
      icon:'none'
    });
    this.setData({
      awardlist:[],
      isaward:false
    });
  }
})